package problem429;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

//429.N叉树的层序遍历
//https://leetcode.cn/problems/n-ary-tree-level-order-traversal/

class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};

class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        Queue<Node> queue = new LinkedList<>();
        List<List<Integer>> ret = new ArrayList<>();
        if(root != null) queue.add(root);
        while(!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> list = new ArrayList<>();
            for(int i = 0; i<size; i++) {
                Node node = queue.poll();
                list.add(node.val);
                for(Node n : node.children) {
                    if(n != null) queue.add(n);
                }
            }
            ret.add(list);
        }
        return ret;
    }
}